package com.itheima.web;

import com.itheima.dto.OrderInfoDto;
import com.itheima.entry.Result;
import com.itheima.service.OrderService;
import com.itheima.vo.OrderSuccessfulVo;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.amqp.AmqpException;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessagePostProcessor;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @author jshstart
 * @creat 2020-11-18 19:51
 */
@RestController
@RequestMapping("/order")
public class OrderController {
    @Resource
    private RabbitTemplate rabbitTemplate;

    @Reference(mock = "com.itheima.mock.OrderServiceMock")
    private OrderService orderService;


    @GetMapping("/sendCode/{telephone}")
    public void sendCode(@PathVariable("telephone") String telephone){
        rabbitTemplate.convertAndSend("sms_exchange","sms",telephone);
    }


    @PostMapping("/submitOrder")
    public Result submitOrder(@RequestBody OrderInfoDto orderInfoDto){
        Long orderNo=orderService.submitOrder(orderInfoDto);
        if (orderNo==null){
            return new Result(false,"页面火爆，请再次刷新！");
        }
       return new Result(orderNo);
    }

    @GetMapping("/orderSuccessful/{id}")
    public Result findOrderInfo(@PathVariable("id") Long id){
       OrderSuccessfulVo orderSuccessfulVo=orderService.findOrderInfo(id);
        if (orderSuccessfulVo==null){
            return new Result(false,"页面火爆，请再次刷新！");
        }
       return new Result(orderSuccessfulVo);
    }

    @GetMapping("isSuccess/{orderNo}")
    public void isSuccess(@PathVariable String orderNo){
        MessagePostProcessor mes=new MessagePostProcessor() {
            @Override
            public Message postProcessMessage(Message message) throws AmqpException {
                message.getMessageProperties().setExpiration("30000");
                return message;
            }
        };
        rabbitTemplate.convertAndSend("payEX","pay",orderNo,mes);
    }
}
